import request from '@/utils/request'

export function login(data) {
  return request({
    url: '/admin/auth/sign_in',
    method: 'post',
    data
  })
}

export function register(data) {
  return request({
    url: '/admin/auth/sign_up',
    method: 'post',
    data
  })
}

// 新增用户
export function createUser(data) {
  return request({
    url: '/admin/users',
    method: 'post',
    data
  })
}

// 查询单条
export function fetchUser(id) {
  return request({
    url: `/admin/users/${id}`,
    method: 'get'
  })
}

// 修改用户
export function updateUser(id, data) {
  return request({
    url: `/admin/users/${id}`,
    method: 'put',
    data
  })
}

export function getInfo(token) {
  return request({
    url: '/vue-admin-template/user/info',
    method: 'get',
    params: { token }
  })
}

export function logout() {
  return request({
    url: '/vue-admin-template/user/logout',
    method: 'post'
  })
}

// 修改密码
export function updatePassword(data) {
  return request({
    url: '/admin/home',
    method: 'put',
    data
  })
}

// 查询用户列表
export function fetchUserList(params) {
  return request({
    url: '/admin/users',
    method: 'get',
    params
  })
}

// 修改管理员状态
export function toggleAdmin(id) {
  return request({
    url: `/admin/users/${id}/toggle_admin`,
    method: 'patch'
  })
}

// 删除用户
export function deleteUser(id) {
  return request({
    url: `/admin/users/${id}`,
    method: 'delete'
  })
}

// 查看订单
export function showUserOrders(id) {
  return request({
    url: `/admin/users/${id}/show_orders`,
    method: 'get'
  })
}
